<script setup>
import { ref } from 'vue'
import { userUpdatePasswordService } from '@/api/user.js'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/modules/user.js'
import { useRouter } from 'vue-router'

const formRef = ref()
const passwdModel = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})
const loading = ref(false)
const validate1 = () => {
  // 自定义校验规则，新密码和旧密码不能一样
  const new_pwd = passwdModel.value.new_pwd
  const old_pwd = passwdModel.value.old_pwd
  if (new_pwd === old_pwd) {
    return new Error('新密码不能和旧密码一样')
  }
}
const validate2 = () => {
  // 自定义校验规则，新密码和确认密码必须一样
  const new_pwd = passwdModel.value.new_pwd
  const re_pwd = passwdModel.value.re_pwd
  if (new_pwd !== re_pwd) {
    return new Error('新密码和确认密码必须一样')
  }
}
const rules = {
  old_pwd: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]{6,12}$/, message: '密码由6到12位字母或数字组成', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
    // 6到12为非空字符
    { pattern: /^[a-zA-Z0-9]{6,12}$/, message: '密码由6到12位字母或数字组成', trigger: 'blur' },
    { validator: validate1, trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' },
    { validator: validate2, trigger: 'blur' }
  ]
}
const userStore = useUserStore()
const router = useRouter()
const onSubmit = () => {
  loading.value = true
  //   更新密码请求
  userUpdatePasswordService(passwdModel.value)
    .then((r) => {
      loading.value = false
      if (r.data.code !== 0) {
        ElMessage({
          type: 'error',
          message: r.data.message
        })
      } else {
        //   更新成功提示
        ElMessage({
          type: 'success',
          message: '密码修改成功，请重新登录！'
        })
        userStore.setUserInfo({})
        userStore.setToken('')
        router.replace('/login')
      }
    })
    .catch((err) => {
      loading.value = false
      ElMessage({
        type: 'error',
        message: err.message
      })
    })
}
const onCancel = () => {
  formRef.value.resetFields()
}
</script>

<template>
  <el-card class="box-card" shadow="hover">
    <template #header>
      <div class="card-header">
        <span>修改密码</span>
      </div>
    </template>
    <el-form
      v-loading="loading"
      ref="formRef"
      :model="passwdModel"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="旧密码" prop="old_pwd">
        <el-input v-model="passwdModel.old_pwd" placeholder="请输入旧密码" type="password" />
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="passwdModel.new_pwd" placeholder="请输入新密码" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="re_pwd">
        <el-input v-model="passwdModel.re_pwd" placeholder="请再次输入新密码" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button @click="onCancel">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style scoped>
.box-card {
  max-width: 100%;
  margin: 20px;
  min-height: 90%;
}
</style>
